namespace BM.Tools.WebControls.MForm.Controls.Titles
{
    using System.ComponentModel;

    using Additions;

    using BM.Tools.WebControls.MForm.Controls.Helper;
    using BM.Tools.WebControls.MForm.Controls.Items;

    /// <summary>
    /// The title control that is used inside the MForm items    
    /// </summary>
    [DesignTimeVisible(false)]
    public class FormTitleControl : TitleControl
    {        
        private readonly MinimizeButton minimizeButton;
        
        private readonly OffspringItem owner;

        /// <summary>
        /// Initializes a new instance of the <see cref="FormTitleControl"/> class.
        /// </summary>
        public FormTitleControl()
        {
            minimizeButton = new MinimizeButton { Visible = false };
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FormTitleControl"/> class.
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public FormTitleControl(OffspringItem owner) : this()
        {
            this.owner = owner;
        }

        /// <summary>
        /// Gets the minimize button.
        /// </summary>        
        public MinimizeButton MinimizeButton
        {
            get
            {
                return minimizeButton;
            }
        }

        /// <summary>
        /// Gets or sets the text of the title.
        /// If no text is specified, take the display name of the owner control
        /// </summary>        
        public override string Text
        {
            get
            {
                return base.Text ?? owner.DisplayName;
            }
        }

        /// <summary>
        /// add Minimize button and removal button
        /// </summary>        
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Controls.Add(MinimizeButton);
            Controls.Add(new RemovalButton(owner));
        }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data. </param>
        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);
            owner.Additions.Add(new TitleAddition());
        }
    }
}